package com.itmake.sample.repeat.config;

import com.itmake.sample.repeat.filter.RepeatFilter;
import com.itmake.sample.repeat.interceptor.RepeatInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 重复提交配置
 *
 * @author Amjacks
 * @since 2023/08/10
 */
@Configuration
public class RepeatConfig implements WebMvcConfigurer {

    @Autowired
    private RepeatInterceptor repeatInterceptor;

    /**
     * 注解拦截器，判定是否重复提交
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(repeatInterceptor).addPathPatterns("/**");
    }

    /**
     * 注册过滤器，包装请求
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new RepeatFilter());
        registration.addUrlPatterns("/*");
        registration.setName("repeatFilter");
        registration.setOrder(Ordered.LOWEST_PRECEDENCE);
        return registration;
    }
}
